package com.hzhjhh.view.manageView.menus.accountMng.register;

import java.util.Objects;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.constant.DateFormatContants;
import com.hzhjhh.entity.UserInfo;
import com.hzhjhh.mapper.UserInfoMapper;
import com.hzhjhh.means.Select_Means;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.accountMng.RegisterDialog;

import cn.hutool.core.date.DateUtil;

/**
 * 菜单栏初始化-仓库-账户管理-添加/修改用户-管理员登录弹出框-退出用户弹出框-查询按钮
 * 
 * @author 12083
 *
 */
public class QueryBt {
	
	private static UserInfoMapper userInfoMapper;

	/**
	 * 按钮
	 */
	public static JButton btInit(RegisterDialog registerDialog) {
		JButton query = new JButton();
		query.setBounds(175, 279, 83, 25);
		query.setText("查询");

		query.addActionListener(event -> {
			JTextField txfCardId = registerDialog.getTxfCardId();
        	String cardId = txfCardId.getText(); //卡号
        	UserInfo userInfo = userInfoMapper.selectByCardId(cardId);
//            Vector Select_vector = new Vector();
//            Select_vector = Select_Means.Select_UserManager(cardId); //查询此卡是否存在
            if (Objects.isNull(userInfo)) {
                JOptionPane.showMessageDialog(registerDialog, "卡号不存在", "提示",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            txfCardId.setText(userInfo.getCardId().toString()); //卡号
            registerDialog.getTxfCardOwner().setText(userInfo.getUserName()); //持卡人
            registerDialog.getTxfIDCard().setText(userInfo.getUserId()); //身份证
            registerDialog.getTxfCardOpenDate().setText(DateUtil.format(userInfo.getCardDate(), DateFormatContants.YYYY_MM_DD_HH_MM_SS)); //开卡日期
            JComboBox<String> cobUserType = registerDialog.getCobUserType();
            if (StringUtils.equalsAny(userInfo.getUserType(), "普通会员", "收银员", "管理员")) {
            	cobUserType.removeAllItems();
                cobUserType.addItem("普通会员");
                cobUserType.addItem("收银员");
                cobUserType.addItem("管理员");
			}
//            if (Select_vector.get(5).toString().equals("普通会员")) {
//                cobUserType.removeAllItems();
//                cobUserType.addItem("普通会员");
//                cobUserType.addItem("收银员");
//                cobUserType.addItem("管理员");
//            }
//            if (Select_vector.get(5).toString().equals("收银员")) {
//                cobUserType.removeAllItems();
//                cobUserType.addItem("收银员");
//                cobUserType.addItem("普通会员");
//                cobUserType.addItem("管理员");
//            }
//            if (Select_vector.get(5).toString().equals("管理员")) {
//                cobUserType.removeAllItems();
//                cobUserType.addItem("管理员");
//                cobUserType.addItem("收银员");
//                cobUserType.addItem("普通会员");
//            }
            registerDialog.getTxfTerm().setText(userInfo.getTerm()); //期限
            registerDialog.getTxfScore().setText(userInfo.getScore().toString()); //积分
            registerDialog.getTxfDiscount().setText(userInfo.getDiscount().toString()); //折扣
            registerDialog.getTxaRemark().setText(userInfo.getRemark()); //备注
        });
		
		// 初始化mapper
		SqlSession sqlSession = DBUtils.getSqlSession(true);
		userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
		
		return query;
	}
}
